<template>
	<view class="">

		<view class="content">
			<u--form labelPosition="left" :model="reqData" :rules="rules" ref="editform1" :style="{width: '95%' }"
				class="content">
				<u-form-item :labelWidth="90" label="真实姓名" prop="uname" borderBottom>
					<view >
						<input maxlength="15" v-model="reqData.uname" class="uni-input" placeholder="请输入真实姓名"
							@blur="reqData.uname = reqData.uname.replace(/\s*/g,'')"></input>
					</view>
				</u-form-item>
				<u-form-item label="电话号码" :labelWidth="90" prop="phone" borderBottom>
					<view >
						<input maxlength="11" v-model="reqData.phone" type="number" placeholder="请输入电话号码"></input>
					</view>
				</u-form-item>
				<u-form-item label="身份证" :labelWidth="90" prop="identity" borderBottom>
					<view>
						<input v-model="reqData.identity" type="number" placeholder="请输入身份证"></input>
					</view>
				</u-form-item>
				<u-form-item label="住址" :labelWidth="90" prop="home" borderBottom>
					<view >
						<u--textarea v-model="reqData.home" placeholder="请输入住址" ></u--textarea>
					</view>
				</u-form-item>
			</u--form>
		</view>
		<at-submit-btn txt="提交" @submit="submit"></at-submit-btn>
	</view>
</template>

<script>
	import AtSubmitBtn from '@/components/AtSubmitBtn'
	import {usereditinfo} from '@/common/http.api.js'
	export default {
		components: {
			AtSubmitBtn
		},
		data() {
			return {
				rules: {
					uname: {
						required: true,
						message: '必填'
					},
					phone: {
						required: true,
						message: '必填,且是数字',
						type: 'number',

					},
					identity: {
						required: true,
						message: '必填，且是数字',
						type: 'number',
					},
					home: {
						required: true,
						message: '必填',
					}
				},
				reqData: {
					uname: '',
					phone: '',
					home: '',
					identity: ''
				}
			};
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.editform1.setRules(this.rules)
		},
		onLoad() {
			this.reqData = {...this.$store.state.userInfo}
		},
		methods: {
			submit() {
				this.$refs.editform1.validate().then(async () => {
					uni.$loading()
					// 处理换行
					const res = await  usereditinfo(this.reqData)
					if (res.code == 0) uni.$showMsg('编辑成功', 'success', 1500)
					this.$store.commit('getUserInfo', this.reqData)
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/user/user'
						})
					}, 1500)

				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 90%;
		margin: auto;
	}
</style>
